package cd.safe.school.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import cd.safe.school.R;
import cd.safe.school.adapter.MusicAdapter;
import cd.safe.school.adapter.MusicImageAdapter;
import cd.safe.school.util.XmlUtil;

public class EngilshActivity extends Activity implements OnClickListener {
	private MediaPlayer mp;
	private Button start_pause;
	private SeekBar mien_sb;
	private Timer timer;
	private TextView play_position;
	private TextView total_position;
	private Handler handler = new Handler();
	private Gallery music_image;
	private List<Bitmap> lists = new ArrayList<Bitmap>();
	private List<String> images;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.englishdictation_01);
		music_image = (Gallery) findViewById(R.id.music_image);
		start_pause = (Button) findViewById(R.id.start_pause);

		mien_sb = (SeekBar) findViewById(R.id.mien_sb);
		play_position = (TextView) findViewById(R.id.play_position);
		total_position = (TextView) findViewById(R.id.total_position);
		String music_path = getIntent().getStringExtra("music_path");
		images = XmlUtil.imgnames;
		for (String s : images) {
			String musicpath = music_path + "/" + s;
			Bitmap bt = BitmapFactory.decodeFile(musicpath);
			lists.add(bt);

		}

		MusicImageAdapter adapter = new MusicImageAdapter(this, lists);
		music_image.setAdapter(adapter);

		mp = new MediaPlayer();
		play_position.setText("00:00");
		String path = getIntent().getStringExtra("music_path");
		String tag = getIntent().getStringExtra("tag");
		if ("composition".equals(tag)) {
			HomeActivity.rutn = 8;
		} else if ("synparse".equals(tag)) {
			HomeActivity.rutn = 9;
		} else if ("error".equals(tag)) {
			HomeActivity.rutn = 10;

		}

		else {
			HomeActivity.rutn = 3;
		}

		if (path != null && !"".equals(path)) {
			start_pause.setOnClickListener(this);
			try {

				mp.setDataSource(path);
				mp.prepare();
				mien_sb.setMax(mp.getDuration());
				total_position.setText(PostionToTime(mp.getDuration()));

			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		mien_sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				int a = seekBar.getProgress();
				mp.seekTo(a);
				mp.start();

			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				if (mp.isPlaying()) {
					mp.pause();

				}

			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {

			}
		});
		mp.setOnCompletionListener(new OnCompletionListener() {

			@Override
			public void onCompletion(MediaPlayer mp) {
				timer.cancel();
				mien_sb.setProgress(0);

				play_position.setText("00:00");

			}
		});

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.start_pause:
			if (mp.isPlaying()) {

				mp.pause();
			} else {
				start_pause.setBackgroundResource(R.drawable.music_start);
				mp.start();
				timer = new Timer();
				timer.schedule(new TimerTask() {

					@Override
					public void run() {

						handler.post(new Runnable() {

							@Override
							public void run() {
								int currentpostion = mp.getCurrentPosition();
								mien_sb.setProgress(currentpostion);
								play_position
										.setText(PostionToTime(currentpostion));

							}
						});

					}
				}, 0, 300);

			}

			break;

		}

	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
		if (timer != null) {
			timer.cancel();
		}

		mp.stop();
		mp.release();
	}

	public String PostionToTime(int time) {
		time /= 1000;
		int minute = time / 60;
		int hour = minute / 60;
		int sencond = time % 60;
		return String.format("%02d:%02d", minute, sencond);

	}

	@Override
	protected void onPause() {
		if (timer != null) {
			if (mp.isPlaying())
				mp.pause();

		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (timer != null) {
			if (!mp.isPlaying())
				mp.start();

		}
		super.onResume();
	}

}
